<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>查询标签</title>
	</head>
    <link rel="stylesheet" href="css/AllStyle.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <script type="text/javascript" src="js/common.js" ></script>
    <script type="text/javascript" src="js/data.js" ></script>
    <script type="text/javascript" src="js/tmpl.js" ></script>
	<body>
		<!-- 页头 -->
	    <section>
		    <script type="text/javascript">
		        AllPageHeader()
		    </script>
	    </section>
	    
		<!-- 页面-->
    	<section class="AllContent">
    		<div class="ScanBlock">
	    	<form>
	    		<div class="ScanInput">
					<p><input type="text" id="query"
                           onkeypress="if (event.keyCode == 13) {
                                       proccessTextArea(this);
                                       return false;
                                   }"
                           placeholder="请扫描条码" ></p>
					<p><button type="button" onclick="proccessTextArea(this);">提交</button></p>
				</div>
			</form>
			</div>
			
			<p id="BlurbLoading" style="background: #fff;overflow: hidden;margin-top:10px;display: none;"><img width="100px;" style="display:block;margin:30px auto;" src="images/loading5.gif"></p>
			<div class="Blurb">
				<div class="BlurbInfo">
					<ul>
						<li>商品名称：</li>
						<li id="prdname" style="overflow: hidden;white-space:nowrap;text-overflow:ellipsis;">null</li>
						<li>商品编号：</li>
						<li id="itemNo">null</li>
					</ul>
				</div>
				<div class="FruitTraceability">
					<span>产品溯源过程<a></a></span>
					<div>
						<ul id="datalistid">
							<li>
								<p>{{track.from_name}} </p>
								<p>{{track.start_date}}</p>
								<i></i>
							</li>
						</ul>
					</div>
				</div>
			</div>
    	</section>
    	
	</body>
	<script type="text/javascript" src="js/jquery.min.js" ></script>
	<script type="text/javascript" src="js/mui.min.js" ></script>
    <script type="text/javascript" src="js/echarts/common.js" ></script>
	<script type="text/javascript">
	    mui.init();
	    
		$('.AllHeader li:eq(1)').text('查询标签')
		$('.AllHeader li:eq(2)').html('<img style="width:28px" src="images/Scan.png">')
		
		mui.plusReady(function(){
		    //扫描器
			document.getElementById('Scanner').addEventListener('tap', function() {
			  mui.openWindow({
			    url: 'Scanner.html?etk=2', 
			    id:'Scanner.html'
			  });
			});
		});
		
		$(function(){
	    	$("#query").focus();
	    })
		
		$(function(){
			loadConfig();
		})
		
		function loadConfig(){
	        $.ajax({
	            type: "post",
	            url: getServerUrl() + "/Open/ComSys/sysparam", //与此php页面沟通
	            dataType: 'json', //从php返回的值以 JSON方式 解释
	            data: 'access_token='+getToken(), //
	            success: function (json) {//如果调用php成功
	
	                prefix = json.code_prefix;
	
	            },
	            error:function(json){
	                alertMessage(L(json.error_code));
	            }
	        });
	    }
		
		//添加refresh自定义事件监听
		window.addEventListener('query',function(){
			var id = event.detail.id;
			$('#query').val(id);
			proccessTextArea($('#query'));
		});
		
		$(function () {
	        if (!empty(getCookie("itemcode"))) {
	            $("#query").val(getCookie("itemcode"));
	            proccessTextArea();
	        }
	    });
	
	    function proccessTextArea(t) {
			var item_no = getItemNo($(t).val());
			$(t).val(item_no);
	        var q = $("#query").val();
	        if (empty(q)) {
	            alertMessage("请输入条形码！");
	        } else {
	        	if(q.length < 15){
            		q = prefix+q;
            	}
	        	$('#BlurbLoading').show();
	            $.ajax({//一个Ajax过程
	                type: "post", //以post方式与后台沟通
	                url: getServerUrl() + "/Open/Comtag/queryItem/",
	                dataType: 'json', //从php返回的值以 JSON方式 解释
	                data: 'item_no=' + q+'&access_token='+getToken(),
	                success: function (json) {//如果调用php成功
	                    if (json.result == "1") {
	                        $("#itemNo").text(json.pid);
	                        $("#prdname").text(json.pname);
	                        var html = "";
													var fromName = "";
													var lastTime = ""
	                        if(!empty(json.trackinglist)) {
	                            for (var i = 0; i < json.trackinglist.length; i++) {
	                                html = html + '<li>'
	                                + '<p>' + json.trackinglist[i].to_name + '</p>'
	                                + '<p>' + json.trackinglist[i].start_date.substring(0, 10) + '</p>'
	                                + '<i></i>'
	                                + '</li>';
	                            }
															 fromName = json.trackinglist[json.trackinglist.length-1].from_name;
															 lastTime = json.trackinglist[json.trackinglist.length-1].start_date.substring(0, 10);
	                        }
	                        	
	       				 	$('#BlurbLoading').hide(300);
	                        $("#datalistid").html(html);
	                        $('#datalistid').append('<li><p>'+fromName+'</p><p>'+lastTime+'</p><i></i></li>')
	                        $('.Blurb').show(300);
	                        if(json.trackinglist == null) {
	                        	$('.FruitTraceability').hide();
	                        }
	                        
	                    } else {
	                        //hideLoading();
	                        alertMessage("查询数据异常:[" + json.error_description + "]");
	       				 	$('#BlurbLoading').hide(300);
	                        $('.Blurb').hide(300);
	                    }
	                    $('#query').val('');
	                }
	            });
	        }
	    }
	
	    function onDeviceReady() {
	        document.addEventListener("backbutton", onBackKeyDown, false);
	    }
	    function onBackKeyDown() {
	        openUrl("index.html");
	    }
		
	</script>
</html>
